/**
 * 
 */
package util;

/**
 * @author user
 *
 */

import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import java.util.ArrayList;
import java.io.FileInputStream;
import javax.swing.JLabel;

import java.net.SocketException;

public class MyFtp 
{
    private String url;
    private String user;
    private String password;
    private FTPClient ftp;
    private String workingDirectory;
    private ArrayList<String> files;
    private String msgError;
    private JLabel lblMsg;
    
    public MyFtp(JLabel lbl)
    {
            ftp = new FTPClient();
            files = new ArrayList<String>();
            lblMsg = lbl;
    }

    public Boolean connect()
    {
            try
            {
                ftp.connect( getUrl() );

                ftp.login( getUser(),getPassword()  );

                ftp.changeWorkingDirectory ( getWorkingDirectory() );

                return true;
            }
            catch(IOException e)
            {
                msgError = e.getMessage();
                return false;
            }
    }

    public Boolean sendFiles()
    {
        try
        {
            for(String file: files)
            {
                FileInputStream arqEnviar = new FileInputStream(file);

                lblMsg.setText(file);
                file= file.replace(':' , '-'); 
                file= file.replace('\\', '-');
                //file= file.replace(' ' , '%');
                //System.out.println(file);

                  if (!ftp.storeFile (file, arqEnviar))
                  {
                     msgError= "Arquivo não foi armazenado, verifique as propriedades do diretório!";  
                  }
            }
            return true;
        }
        catch( SocketException ee)
        {
            msgError = ee.getMessage();
            return false;			
        }		
        catch( IOException e)
        {
            msgError = e.getMessage();
            return false;
        }
    }

    public void addFile(String file)
    {
        files.add(file);
    }

    public int countFiles()
    {
        return files.size();
    }

    public String getError()
    {
        return getMsgError();
    }

    public Boolean disconnect()
    {
        try
        {
                ftp.logout();
                ftp.disconnect();
                return true;
        }
        catch(IOException e)
        {
                msgError = e.getMessage();
                return false;
        }
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getWorkingDirectory() {
        return workingDirectory;
    }

    public void setWorkingDirectory(String workingDirectory) {
        this.workingDirectory = workingDirectory;
    }

    public String getMsgError() {
        return msgError;
    }
}
